const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    userId: '',
    currentIndex: 0,
    imgUrl: app.globalData.imgUrl,
    types: [],
    planting: [],
    fertilizer: [],
    irrigate: [],
    insectAttack: [],
    harvest: [],
    cropProduce: [],
    storage: [],
    steps: ['品种', '种植', '施肥', '灌溉', '病虫处理', '采收', '加工', '储存'],
    siteIds:[]
  },
  onLoad() {
    let that = this;
    that.setData({
      types: [],
      planting: [],
      fertilizer: [],
      irrigate: [],
      insectAttack: [],
      harvest: [],
      cropProduce: [],
      storage: []
    })
    wx.getStorage({
      key: 'userId',
      success: function(res) {
        that.setData({
          userId: res.data
        });
        // that.queryCropType();
        // that.queryPlanting();
        // that.queryFertilizer();
        // that.queryIrrigate();
        // that.queryInsectAttack();
        // that.queryHarvest();
        // that.queryCropProduce();
        // that.queryStorage();
      },
    })
    wx.getStorage({
      key: 'role',
      success(res) {
        if(res.data === 1) {
          that.getCropOriginData()
        } else {
          // 获取基地
          api.queryPlantation({
            data: {
              account: that.data.userId
            },
            success(res) {
              let siteIds = res.data.plantation.map(item => {
                return item.siteId
              })
              that.setData({
                siteIds
              })
              that.getCropOriginData_admin(0)
            }
          })
        }
      }
    })
  },
  getCropOriginData() {
    let that = this
    api.getCropOriginData({
      data: {
        account: that.data.userId
      },
      success(res) {
        that.formatData(res)
      }
    })
  },
  getCropOriginData_admin(count) {
    let that = this
    if(count === that.data.siteIds.length){
      return
    }
    api.getCropOriginData_admin({
      data: {
        siteId: that.data.siteIds[count]
      },
      success(res) {
        that.formatData(res)
        that.getCropOriginData_admin(++count)
      }
    })
  },
  formatData(res) {
    let that = this
    let data = res.data

    let types = that.data.types
    data.cropType_s.map(i => {
      i.image = that.data.imgUrl + 'cropTypeImage/' + i.image
    })
    types.push(...data.cropType_s)

    let planting = that.data.planting
    data.planting.map(i => {
      i.image = that.data.imgUrl + 'plantingImage/' + i.image;
    })
    planting.push(...data.planting)

    let fertilizer = that.data.fertilizer
    data.fertilizer.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'fertilizerImage/' + i.image;
      })
      fertilizer.push(...item)
    })
    let irrigate = that.data.irrigate
    data.irrigate.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'irrigateImage/' + i.image;
      })
      irrigate.push(...item)
    })
    let insectAttack = that.data.insectAttack
    data.insectAttack.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'insectAttackImage/' + i.image;
      })
      insectAttack.push(...item)
    })
    let harvest = that.data.harvest
    data.harvest.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'harvestImage/' + i.image;
      })
      harvest.push(...item)
    })
    let cropProduce = that.data.cropProduce
    data.cropProduce.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'cropProduceImage/' + i.image;
      })
      cropProduce.push(...item)
    })
    let storage = that.data.storage
    data.storage.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'storageImage/' + i.image;
      })
      storage.push(...item)
    })
    that.setData({
      types,
      planting,
      fertilizer,
      irrigate,
      insectAttack,
      harvest,
      cropProduce,
      storage
    })
  },
  swiper(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      currentIndex: Number.parseInt(index)
    });
  },
  change(e) {
    this.setData({
      currentIndex: e.detail.current
    });
  },
  queryCropType() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'cropTypeImage/';
    let userId = this.data.userId;
    api.queryCropType({
      data: {
        cropTypeId: -1,
        account: userId
      },
      success(res) {
        console.log(res.data);
        let types = res.data.CropType;
        types.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          types
        });
      }
    });
  },
  queryPlanting() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'plantingImage/';
    let userId = this.data.userId;
    api.queryPlanting({
      data: {
        plantId: '',
        cropTypeId: -1,
        siteId: -1,
        placeId: -1,
        account: userId
      },
      success(res) {
        console.log(res.data);
        let planting = res.data.Planting;
        planting.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          planting
        });
      }
    });
  },
  queryFertilizer() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'fertilizerImage/';
    let userId = this.data.userId;
    api.queryFertilizer({
      data: {
        fertilizeId: -1,
        account: userId,
        plantId: ''
      },
      success(res) {
        console.log(res.data);
        let fertilizer = res.data.Fertilizer;
        fertilizer.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          fertilizer
        });
      }
    });
  },
  queryIrrigate() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'irrigateImage/';
    let userId = this.data.userId;
    api.queryIrrigate({
      data: {
        irrigateId: -1,
        plantId: '',
        account: userId,
        type: 'crop'
      },
      success(res) {
        console.log(res.data);
        let irrigate = res.data.Irrigate;
        irrigate.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          irrigate
        });
      }
    });
  },
  queryInsectAttack() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'insectAttackImage/';
    let userId = this.data.userId;
    api.queryInsectAttack({
      data: {
        insectAttackId: -1,
        plantId: '',
        account: userId
      },
      success(res) {
        console.log(res.data);
        let insectAttack = res.data.InsectAttack;
        insectAttack.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          insectAttack
        });
      }
    });
  },
  queryHarvest() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'harvestImage/';
    let userId = this.data.userId;
    api.queryHarvest({
      data: {
        harvestId: '',
        plantId: '',
        type: 'crop',
        account: userId
      },
      success(res) {
        console.log(res.data);
        let harvest = res.data.Harvest;
        harvest.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          harvest
        });
      }
    });
  },
  queryCropProduce() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'cropProduceImage/';
    let userId = this.data.userId;
    api.queryCropProduce({
      data: {
        cropProduceId: '',
        harvestId: '',
        goodId: -1,
        type: 'crop',
        account: userId
      },
      success(res) {
        console.log(res.data);
        let cropProduce = res.data.CropProduce;
        cropProduce.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          cropProduce
        });
      }
    });
  },
  queryStorage() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'storageImage/';
    let userId = this.data.userId;
    api.queryStorage({
      data: {
        storageId: '',
        produceId: '',
        warehouseId: -1,
        type: 'crop',
        account: userId
      },
      success(res) {
        console.log(res.data);
        let storage = res.data.Storage;
        storage.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          storage
        });
      }
    });
  },
  goToAddFormPage() {
    let currentIndex = this.data.currentIndex;
    wx.navigateTo({
      url: `/pages/crop-addform/crop-addform?currentIndex=${currentIndex}`,
    })
  },
  goToDetailPage(e) {
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/info-display/info-display?id=${id}&type=${type}`,
    })
  }
})